import 'package:shared_preferences/shared_preferences.dart';
import '../../core/api_response.dart';
import '../../providers/mood_provider.dart';
import '../../models/love_quote.dart';
import '../../services/love_quote_service.dart';
import '../mood_repository.dart';

class ApiMoodRepository implements MoodRepository {
  final LoveQuoteService _loveQuoteService;

  ApiMoodRepository({LoveQuoteService? loveQuoteService})
      : _loveQuoteService = loveQuoteService ?? LoveQuoteService();

  @override
  Future<ApiResponse<MoodType?>> getMyMood() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final moodIndex = prefs.getInt('my_mood');
      
      if (moodIndex != null && moodIndex < MoodType.values.length) {
        return ApiResponse.success(data: MoodType.values[moodIndex]);
      }
      
      return ApiResponse.success(data: null);
    } catch (e) {
      return ApiResponse.error(error: '获取我的心情失败: $e');
    }
  }
  
  @override
  Future<ApiResponse<MoodType?>> getPartnerMood() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final moodIndex = prefs.getInt('partner_mood');
      
      if (moodIndex != null && moodIndex < MoodType.values.length) {
        return ApiResponse.success(data: MoodType.values[moodIndex]);
      }
      
      return ApiResponse.success(data: null);
    } catch (e) {
      return ApiResponse.error(error: '获取伴侣心情失败: $e');
    }
  }
  
  @override
  Future<ApiResponse<DateTime>> getLastMoodUpdate() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final lastUpdateString = prefs.getString('last_mood_update');
      
      if (lastUpdateString != null) {
        return ApiResponse.success(data: DateTime.parse(lastUpdateString));
      }
      
      return ApiResponse.success(data: DateTime.now());
    } catch (e) {
      return ApiResponse.error(error: '获取最后更新时间失败: $e');
    }
  }
  
  @override
  Future<ApiResponse<void>> setMyMood(MoodType mood) async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final now = DateTime.now();
      
      await prefs.setInt('my_mood', mood.index);
      await prefs.setString('last_mood_update', now.toIso8601String());
      
      return ApiResponse.success();
    } catch (e) {
      return ApiResponse.error(error: '设置我的心情失败: $e');
    }
  }
  
  @override
  Future<ApiResponse<void>> setPartnerMood(MoodType mood) async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final now = DateTime.now();
      
      await prefs.setInt('partner_mood', mood.index);
      await prefs.setString('last_mood_update', now.toIso8601String());
      
      return ApiResponse.success();
    } catch (e) {
      return ApiResponse.error(error: '设置伴侣心情失败: $e');
    }
  }
  
  @override
  Future<ApiResponse<LoveQuote>> getDailyLoveQuote() async {
    try {
      final response = await _loveQuoteService.getDailyQuote();
      
      if (response.isSuccess && response.data != null) {
        return ApiResponse.success(data: response.data!);
      } else {
        final fallbackQuote = LoveQuote(
          id: -1,
          content: '今天也要开心哦 😊',
          category: 'default',
          language: 'zh_CN',
          isActive: 1,
          createdBy: 'system',
          createdAt: DateTime.now().millisecondsSinceEpoch,
        );
        return ApiResponse.success(data: fallbackQuote);
      }
    } catch (e) {
      final fallbackQuote = LoveQuote(
        id: -1,
        content: '今天也要开心哦 😊',
        category: 'default',
        language: 'zh_CN',
        isActive: 1,
        createdBy: 'system',
        createdAt: DateTime.now().millisecondsSinceEpoch,
      );
      return ApiResponse.success(data: fallbackQuote);
    }
  }
}